﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DemoFacebookAPI
{
    public partial class ChatForm : Form
    {
        public string User = "";
        private bool IsVisible = false;
        public string UserMessage = "";
        public string BodyMessage = "";

        public ChatForm()
        {
            InitializeComponent();

            textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler
               (textBox1_KeyDown);
            this.FormClosing += chatClosing;
        }

        public void setUser(string User, string Nick)
        {
            this.User = User;
            Text = Nick;
        }

        public void show()
        {
            if (IsVisible == false)
            {
                Show();
                IsVisible = true;

                System.Media.SystemSounds.Beep.Play();
            }
        }
        public void hide()
        {
            if (IsVisible)
            {
                Hide();
                IsVisible = false;
            }
        }

        private void displayMessage(string from)
        {
            if (from == "remote")
            {
                richTextBox1.Text += " " + Text + ": \r\n" + BodyMessage + "\r\n";
                richTextBox1.Select(richTextBox1.Text.Length, 0);
                richTextBox1.ScrollToCaret();
            }
            if (from == "local")
            {
                richTextBox1.Text += " " + Engine.facebookNick + ": \r\n" +
                    Engine.SendMessageBody + "\r\n";
                richTextBox1.Select(richTextBox1.Text.Length, 0);
                richTextBox1.ScrollToCaret();
            }
        }

        //lettura messaggi in arrivo
        public void timer1_Tick(object sender, EventArgs e)
        {
            if (User == (UserMessage + "@chat.facebook.com"))
            {
                show();

                displayMessage("remote");

                UserMessage = "";
            }
        }

        //invio messaggi
        private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && Engine.SendMessage == false &&
                 textBox1.Text != "")
            {
                e.SuppressKeyPress = true;

                Engine.temp_JID_Receiver = User;

                Engine.SendMessageBody = textBox1.Text;

                displayMessage("local");

                textBox1.Clear();

                Engine.SendMessage = true;
            }
        }

        private void ChatForm_Load(object sender, EventArgs e)
        {

        }
        private void chatClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            hide();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button_Send_Click(object sender, EventArgs e)
        {
            if (Engine.SendMessage == false && textBox1.Text != "")
            {
                Engine.temp_JID_Receiver = User;

                Engine.SendMessageBody = textBox1.Text;

                displayMessage("local");

                textBox1.Clear();

                Engine.SendMessage = true;
            }
        }
    }
}
